#include "Type_Define.h"
#include "IO_LINK_Global_Param.h"
#include "FIFO.h"
#include "SMI.h"
#include "stdlib"
#include "CM.h"
#include "Phy_Driver.h"
/********************
     暂时排除在工程外 8.3

****************/

typedef struct {
    uint16_t ArgBlockID=0x0001;
    uint16_t VendorID=0xFFFF;
    uint8_t MasterType=0;
    uint8_t Features_1=0x07;
    uint8_t Features_2=0x00;
    uint8_t MaxNumberOfPorts=PORT_NUM;
    uint8_t PortTypes[PORT_NUM]={5,5,5,5,5,5,5,5};
  }MasterIdent_TYPE;

//保存SMI接口配置的ConfigList
PortConfigList_TYPE SMI_PortConfigList[PORT_NUM]; 
 
 

int SMI_IN_FIFO,SMI_RESULT_FIFO;

SMI_Object_TYPE Readed_SMI_Obj,Readed_Result_Obj;
void SMI_INIT(void){
   SMI_IN_FIFO=fifo_create(6,sizeof(SMI_Object_TYPE));
    SMI_RESULT_FIFO=fifo_create(6,sizeof(SMI_Object_TYPE));
}

//插入SMI_Object到输入FIFO,然后Master进行处理，由网关调用
BOOL Insert_SMI_Object_To_IN_FIFO(uint8_t ClinetID,uint8_t Port ,uint8_t ArgBlockLength,void * ArgBlock){
    SMI_Object_TYPE * SMI_Object=(SMI_Object_TYPE *)malloc(sizeof(SMI_Object_TYPE));
    //提取ArgBlock中的ArgBlockID信息
 

    SMI_Object->ClientID=ClinetID;
    SMI_Object->Port=Port;
    SMI_Object->ExpArgBlockID=0x0001;
    SMI_Object->ArgBlockLength=ArgBlockLength
    SMI_Object->ArgBlock=ArgBlock;
     if(fifo_writeable(SMI_Object_FIFO)){
         fifo_write(SMI_IN_FIFO,(void *)SMI_Object);  
         return True;
     }else
     {
         return False;
     }
     
}


//插入SMI_Object到输入OUT FIFO,发向上游网关,由IO-LINK协议调用
BOOL Insert_SMI_Object_To_Result_FIFO(uint8_t ClinetID,uint8_t Port ,uint8_t ArgBlockLength,void * ArgBlock){
    SMI_Object_TYPE * SMI_Object=(SMI_Object_TYPE *)malloc(sizeof(SMI_Object_TYPE));
    //提取ArgBlock中的ArgBlockID信息
 

    SMI_Object->ClientID=ClinetID;
    SMI_Object->Port=Port;
    SMI_Object->ExpArgBlockID=0x0001;
    SMI_Object->ArgBlockLength=ArgBlockLength
    SMI_Object->ArgBlock=ArgBlock;
     if(fifo_writeable(SMI_Object_FIFO)){
         fifo_write(SMI_IN_FIFO,(void *)SMI_Object);  
         return True;
     }else
     {
         return False;
     }
     
}



//从SMI队列中提取Object,并调用相应的服务函数
void Parase_SMI_FIFO(void){
    if(fifo_readable(SMI_IN_FIFO)==False){
        return; //FIFO队列为空，直接返回
    }
    fifo_read(SMI_IN_FIFO,(void *)Readed_SMI_Obj);
    uint16_t ArgBlockID;
    memcpy(ArgBlockID,Readed_SMI_Obj.ArgBlock,4);
    switch (ArgBlockID)
    {
    case 0x0002:  //自定义的SMI_MasterIdent_Req代码
        SMI_MasterIdent_Req();
        break;
    case 0x8000:
        SMI_PortConfigList_Req(Readed_SMI_Obj.Port,(PortStatusList_TYPE *)Readed_SMI_Obj.ArgBlock);
        break;
    case 0x3000:
        SMI_DeviceWrite_Req(Readed_SMI_Obj.Port,(On_request_Data_TYPE *)Readed_SMI_Obj.ArgBlock);
        break;
    case 0x3001:
        SMI_DeviceRead_Req(Readed_SMI_Obj.Port,(On_request_Data_TYPE *)Readed_SMI_Obj.ArgBlock);
        break;
    case 0x7000:
        SMI_ParServToDS_Req(Readed_SMI_Obj.Port,(DS_object_TYPE * )Readed_SMI_Obj.ArgBlock);
        break;
    case 0x700F://自定义ArgBlockID
       SMI_DSToParServ_Req(Readed_SMI_Obj.Port);
       break;
    case 0x7001:
       SMI_ParamWriteBatch(Readed_SMI_Obj.Port,(DeviceParBatch_TYPE *)Readed_SMI_Obj.ArgBlock);
       break;
    case 0x7002:
       SMI_ParamReadBatch(Readed_SMI_Obj.Port,(IndexList_TYPE *)Readed_SMI_Obj.ArgBlock);
       break;
    case 0x7003:
       SMI_PortPowerOffOn(Readed_SMI_Obj.Port,(PortPowerOffOn_TYPE*)Readed_SMI_Obj.ArgBlock);
       break;
    case 0x1002:
       SMI_PDOut(Readed_SMI_Obj.Port,(PDOut_TYPE *)Readed_SMI_Obj.ArgBlock);
       break;
    case 0x1003:
       SMI_PDInOut(Readed_SMI_Obj.Port,(PDInOut_TYPE*)Readed_SMI_Obj.ArgBlock);
       break;
    case 0x1FFF:
      SMI_PDOutIQ(Readed_SMI_Obj.Port,(PDInQ_TYPE *)Readed_SMI_Obj.ArgBlock);
      break;
   
    default:
        break;
    }
     


      
     
}


void SMI_MasterIdent_Req(void){

}

void SMI_PortConfigList_Req(uint8_t Port,PortConfigList_TYPE * PortConfigList){
    SMI_PortConfigList[Port].DeviceID=PortConfigList->DeviceID;
    SMI_PortConfigList[Port].IQ_behavior=PortConfigList->IQ_behavior;
    SMI_PortConfigList[Port].PortCycleTime=PortConfigList->PortCycleTime;
    SMI_PortConfigList[Port].PortMode=PortConfigList->PortMode;
    SMI_PortConfigList[Port].Validation_and_Backup=PortConfigList->Validation_and_Backup;
    SMI_PortConfigList[Port].VendorID=PortConfigList->VendorID;
    //通知CM
    Event_CM_PortConfigList_changed(Port);

}

void SMI_DeviceWrite_Req(uint8_t Port,On_request_Data_TYPE * On_Request_Data){

}

void SMI_DeviceRead_Req(uint8_t Port,On_request_Data_TYPE * On_Request_Data){

}

void SMI_ParServToDS(uint8_t Port,DS_object_TYPE * DS_Data){

}

void SMI_DSToParServ_Req(uint8_t Port){

}

void SMI_ParamWriteBatch(uint8_t Port,DeviceParBatch_TYPE *DeviceParBatch){

}

void SMI_ParamReadBatch(uint8_t Port,IndexList_TYPE * IndexList){

}

void SMI_PortPowerOffOn(uint8_t Port,PortPowerOffOn_TYPE * PortPowerOffOn){

}

void SMI_PDOut(uint8_t Port,PDOut_TYPE * PDOut){

}

void SMI_PDOutIQ(uint8_t Port,PDOutIQ_TYPE * PDOutIQ){

}